-- UIScrapShop
-- Create by cheny3
-- 佣兵营地兑换/购买罐子界面

require "game/ui/form/shop/UIPetShopGoodsDetail"
require "game/ui/form/shop/UIBuyPuzzles"

-- UIScrapShop继承自Layer
UIScrapShop = class("UIScrapShop", function()
    return cc.Layer:create();
end);

function UIScrapShop.create(tabSelect)
    return UIScrapShop.new(tabSelect);
end

-- 商品格最大行数和列数
local MAX_GRID_ROW    = 2;
local MIN_GRID_COLUMN = 3;

-- 标签页
PETSHOP_TYPE_SCRAP      = 1;  -- 碎片
PETSHOP_TYPE_PUZZLE_BOX = 2;  -- 宝罐

local TAB_TYPE_LIST = {
    PETSHOP_TYPE_SCRAP,       -- 碎片
    PETSHOP_TYPE_PUZZLE_BOX,  -- 宝罐
};

-- 超过一分钟没有更新主动请求数据
local SECONDS_PER_MIN = 60;
-- 最大请求次数
local MAX_TYR_TIME = 10;

-- 内部函数声明

-- 构造函数
function UIScrapShop:ctor(tabSelect)
    -- 初始化
    self:setName("UIScrapShop");
    local node = cc.CSLoader:createNode("layout/shop/BlackMarket.csb");
    self:addChild(node);
    self.node = node;

    local CT = node:getChildByName("CT");
    local scrollview = CT:getChildByName("scroll_view");
    self.CT = CT;
    self.scrollView = scrollview;
    self.scrollView:setVisible(true);

    -- 默认选中标签页
    self.tabSelect = tabSelect;

    -- 水平滑动，需要显示左右蒙版
    local mask3 = findChildByName(self.node, "CT/mask3");
    mask3:setVisible(true);
    local mask4 = findChildByName(self.node, "CT/mask4");
    mask4:setVisible(true);

    -- 修改滚动方向
    -- 1 垂直
    -- 2 水平
    self.scrollView:setDirection(2);

    -- 尝试请求次数
    self.hasTry = 0;

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 初始化
    self:init();

    -- 注册点击事件
    self:registerTouchEvent();

    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIScrapShop:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- mask居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "mask");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 获取标签名称
function UIScrapShop:getTabNameMap()
    local tab_name_map = {
        [PETSHOP_TYPE_SCRAP]      = getLocStr("exchange"),
        [PETSHOP_TYPE_PUZZLE_BOX] = getLocStr("buy"),
    };
    return tab_name_map;
end

-- 初始化标签页
function UIScrapShop:init()
    if self.tabSelect then
        -- 指定标签页
        self.shopType = TAB_TYPE_LIST[self.tabSelect];
    else
        -- 默认选择第一个标签页
        self.shopType = TAB_TYPE_LIST[1];
    end

    self.tabs = {};
    for i, shopType in pairs(TAB_TYPE_LIST) do
        local tabNode  = findChildByName(self.node, "CT/tabs/tab"..i);

        table.insert(self.tabs, tabNode);

        -- 绑定当前的类型
        local icon = findChildByName(tabNode, "icon");
        icon.shopType = shopType;
    end
    self:redrawTabs();

    -- 记录滚动容器原始宽高
    -- 切换标签页时，滚动容器的内部滚动区域可能会发生变化，因此这里需要将原始宽高记录下来
    local scrollview = findChildByName(self.node, "CT/scroll_view");
    self.initInnerHeight = scrollview:getInnerContainerSize().height;
    self.initInnerWidth  = scrollview:getInnerContainerSize().width;

    for i = 1, #self.tabs do
        local tab = self.tabs[i];
        tab:setPositionY(tab:getPositionY() + 100);
    end

    local tabNodes = findChildByName(self.node, "CT/tabs");
    tabNodes:setVisible(true);

    -- tab的动画效果
    performWithDelay(self, function() self:playTabEffect(); end, 0.2);
end

-- 初始化商品格
function UIScrapShop:initGoodsGrids(node)
    local w = DESIGN_WIDTH;
    local h = DESIGN_HEIGHT;

    -- 图标间距(横向)
    local wGap = 23;

    -- 图标间距(纵向)
    local hGap = 33;

    -- 图标宽度
    local iconWidth = 128;

    -- 图片高度
    local iconHeight = 163;

    local topHGap  = 100;
    local leftWGap = 20;

    -- 收集商品信息
    local goodsList = self:gatherGoodsInfo();

    -- 商品的共个数
    local shopCount = #goodsList;

    self.scrollView.rows = {};
    self.scrollView:removeAllChildren();

    if shopCount <= 6 then
        leftWGap = 41;
    end

    local itemNode = cc.CSLoader:createNode("layout/shop/BlackMarketGoodsIcon.csb");
    wGap = 15;

    -- @param windowNode    界面节点
    -- @param scrollNode    容器节点
    -- @param itemNode      单元格的节点
    -- @param totalNum      单元格总数
    -- @param maxRowNum     最大行数
    -- @param pageColNum    单页可以显示下的列数
    -- @param wGap          单元格间距(横向) 这里作为最小值使用
    -- @param hGap          单元格间距(纵向)
    -- @param iconWidth     单元格宽度
    -- @param iconHeight    单元格高度
    -- @param topHGap       上方留
    -- @param leftWGap      左右留白  这里作为最小值使用
    -- @param touchEvent    点击事件注册函数
    initScrollGridsHorizontal(self, self.scrollView, itemNode, shopCount, MAX_GRID_ROW, MIN_GRID_COLUMN, wGap, hGap, iconWidth, iconHeight, topHGap, leftWGap, self.registerPetShopTouchEvent);
end

-- 清空所有的商品格
function UIScrapShop:clearAllGrid()
    -- 格子总数
    local totalGridCount = MAX_GRID_ROW * MIN_GRID_COLUMN;
    for i = 1, totalGridCount do
        local grid = self.scrollView:getChildByName("item" .. i);
        if grid then
            grid:setVisible(false);
        end
    end
end

function UIScrapShop:redraw()
    -- 去除所有
    self.scrollView:removeAllChildren();
    local soldOutTipsLabel = findChildByName(self.node, "CT/sold_out_tips");
    local slimeImage = findChildByName(self.CT, "slime");
    soldOutTipsLabel:setVisible(false);
    slimeImage:setVisible(false);

    -- 初始化格子
    self:initGoodsGrids(self.node);
    -- 绘制详细内部界面
    self:redrawNext();
end

-- 重绘宝罐页面
function UIScrapShop:redrawNext()
    -- 主标题
    local titleLabel = findChildByName(self.CT, "title");
    TextStyleM.setTitleStyle(titleLabel);

    -- 副标题
    local descLabel = findChildByName(self.CT, "subhead");
    TextStyleM.setSubheadStyle(descLabel);

    if self.shopType == PETSHOP_TYPE_PUZZLE_BOX then
        -- 罐子
        titleLabel:setString(getLocStr("buy_tank"));
        descLabel:setString(getLocStr("tank_tip"));
    elseif self.shopType == PETSHOP_TYPE_SCRAP then
        -- 碎片/绘制时间
        titleLabel:setString(getLocStr("scrap_change"));
        self:redrawTime();
    end

    -- 刷新按钮
    local btnRefresh = findChildByName(self.node, "BT/refresh");
    btnRefresh:setVisible(false);

    -- 收集商品信息
    local goodsList = self:gatherGoodsInfo();

    local totalGoodsCount = #goodsList;

    if not goodsList or #goodsList == 0 then
        cclog("收集商品信息失败。");
        self:clearAllGrid();
        return;
    end

    -- 显示商品信息
    local totalGoodsCount = #goodsList;

    -- 绘制商品信息
    self:redrawGoods(goodsList);
end

-- 显示商品信息
function UIScrapShop:redrawGoods(goodsList)
    -- 显示商品信息
    local totalGoodsCount = #goodsList;
    for i = 1, totalGoodsCount do
        local grid          = self.scrollView:getChildByName("item" .. i);
        local goodsBg       = grid:getChildByName("bg");
        local iconImg       = grid:getChildByName("goods_icon");
        local moneyImg      = grid:getChildByName("money_icon");
        local moneyLabel    = grid:getChildByName("money_count");
        local soldOutLabel  = grid:getChildByName("sold_out");
        local rankNode      = grid:getChildByName("star_node");
        local countLabel    = grid:getChildByName("item_count");

        goodsBg:loadTexture("images/ui/frame/normal.png");

        if i <= totalGoodsCount then
            grid:setVisible(true);

            local goodsIndex = goodsList[i][1];   -- 商品序号
            local goodsId    = goodsList[i][2];   -- 商品id
            local classId    = goodsList[i][3];   -- 罐子/碎片的class_id
            local goodsIcon  = goodsList[i][4];   -- 商品图标
            local payAttrib  = goodsList[i][5];   -- 支付属性
            local goodsPrice = goodsList[i][6];   -- 商品价格
            local itemName   = goodsList[i][7];   -- 物品名称
            local rank       = goodsList[i][8];   -- 阶位/兑换碎片的数量

            -- 为商品图标绑定信息
            iconImg.goodsIndex = goodsIndex;
            iconImg.goodsId    = goodsId;
            iconImg.classId    = classId;
            iconImg.goodsIcon  = goodsIcon;
            iconImg.payAttrib  = payAttrib;
            iconImg.goodsPrice = goodsPrice;
            iconImg.itemName   = itemName;
            iconImg.rank       = rank;
            iconImg.goodsType  = self.shopType;

            if self.shopType == PETSHOP_TYPE_SCRAP then
                rankNode:setVisible(false);
                countLabel:setVisible(true);
                TextStyleM.setTextStyle(countLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true, -2);
                countLabel:setString(tostring(rank));
                countLabel:setPositionX(countLabel:getPositionX() - 3);
                countLabel:setPositionY(countLabel:getPositionY() - 1);
            elseif self.shopType == PETSHOP_TYPE_PUZZLE_BOX then
                rankNode:setVisible(false);
                countLabel:setVisible(false);
            end

            -- 显示商品图标/兑换花费
            local icon = "images/ui/market/pet_chip.png";
            local iconPath;
            iconImg:loadTexture(getItemIconPath(goodsIcon));

            moneyImg:setPositionX(moneyImg:getPositionX() - 40);
            moneyImg:setPositionY(moneyImg:getPositionY() + 10);
            moneyLabel:setPositionX(moneyLabel:getPositionX() - 3);
            moneyLabel:setPositionY(moneyLabel:getPositionY() + 10);

            if type(payAttrib) == "string" then
                local iconPath = getSmallAttribIconPath(FieldsM.query(payAttrib, "icon"));
                moneyImg:loadTexture(iconPath);
                moneyLabel:setString(tostring(goodsPrice));
                moneyImg:setScale(0.74);
            else
                if haveItemSmallIcon(payAttrib) then
                    iconPath = getItemSmallIconPath(payAttrib);
                    moneyImg:setScale(1);
                else
                    iconPath = getItemIconPath(payAttrib);
                    moneyImg:setScale(0.45);
                end
                moneyImg:loadTexture(iconPath);
                moneyLabel:setString(tostring(goodsPrice));
                moneyLabel:setPositionY(moneyLabel:getPositionY() - 3);
            end

            TextStyleM.setTextStyle(moneyLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_WHITE, true);
            moneyLabel:getVirtualRenderer():setAdditionalKerning(getLocKerning() -3);
            if type(payAttrib) == "string" then
                if ME.user.dbase:query(payAttrib) < goodsPrice then
                    moneyLabel:setTextColor(TextStyleM.TEXT_COLOR_LIGHT_RED);
                else
                    moneyLabel:setTextColor(TextStyleM.TEXT_COLOR_WHITE);
                end
            else
                if ItemM.getAmount(ME.user, payAttrib) < goodsPrice then
                    moneyLabel:setTextColor(TextStyleM.TEXT_COLOR_LIGHT_RED);
                else
                    moneyLabel:setTextColor(TextStyleM.TEXT_COLOR_WHITE);
                end
            end

            if self.shopType == PETSHOP_TYPE_SCRAP then
                -- 居中显示
                local moneyImgW = moneyImg:getContentSize().width;
                moneyImg:setPositionY(moneyImg:getPositionY() - 2 );
                local moneyLabelW = moneyLabel:getContentSize().width;

                moneyLabel:setPositionX(moneyLabel:getPositionX() + 15);
                moneyLabel:setPositionY(moneyLabel:getPositionY() - 1 );
            else
                local offset = 0;

                if payAttrib == "money" then
                    offset = -8;
                end

                -- 居中显示
                local moneyImgW = moneyImg:getContentSize().width * moneyImg:getScaleX();
                local moneyLabelW = moneyLabel:getContentSize().width;
                -- 间隔
                local gap = 5;
                local s = (moneyImgW + moneyLabelW + gap) / 2;
                -- 位置调整
                moneyImg:setPositionX(-s - offset / 2 );
                moneyLabel:setPositionX(moneyImg:getPositionX() + offset + moneyImgW + gap);
            end

            -- 判断商品已经是否售完
            local soldOut = false;
            if self.shopType == PETSHOP_TYPE_SCRAP then
                if PetShopM.getGoodInfoInShop(goodsId)["sold_out"] == 1 then
                    soldOut = true;
                    iconImg.soldOut = true;
                end
            end

            if soldOut then
                setGrayTransMode(goodsBg, true);
                setGrayTransMode(iconImg, true);

                -- 不显示价格，星星，数量
                moneyImg:setVisible(false);
                moneyLabel:setVisible(false);
                rankNode:setVisible(false);
                countLabel:setVisible(false);

                -- 已兑换字样
                soldOutLabel:setVisible(true);
                TextStyleM.setTextStyle(soldOutLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_ORANGE, true);
                soldOutLabel:getVirtualRenderer():setAdditionalKerning(getLocKerning() -3);
                soldOutLabel:setString(getLocStr("has_been_exchange"));
            else
                -- 背景变亮
                setGrayTransMode(goodsBg, false);
                setGrayTransMode(iconImg, false);

                -- 显示图标、价格、星星
                moneyImg:setVisible(true);
                moneyLabel:setVisible(true);
                rankNode:setVisible(true);

                soldOutLabel:setVisible(false);
            end
        else
            -- 清空多余的格子
            grid:setVisible(false);
        end
    end
end

-- 绘制时间
function UIScrapShop:redrawTime()
    -- 提示信息
    local leftTime = PetShopM.getShopAutoRefreshLeftTime();
    local txt1Label = findChildByName(self.CT, "subhead");

    if self.hasTick then
        TextStyleM.setSubheadStyle(txt1Label);
        txt1Label:setString(string.format(getLocStr("black_market_auto_refresh"), remainingTimeDesc(leftTime)));
        return;
    end

    local function tick()
        if self.shopType ~= PETSHOP_TYPE_SCRAP then
            self.hasTick = false;
            return;
        end

        self.hasTick = true;
        leftTime = PetShopM.getShopAutoRefreshLeftTime();
        if leftTime <= - SECONDS_PER_MIN
            and math.floor(leftTime % 10) == 0 and self.hasTry < MAX_TYR_TIME then
            -- 更新时间超过一分钟，主动请求更新一次
            self:tryRefresh();
        end

        TextStyleM.setSubheadStyle(txt1Label);
        txt1Label:setString(string.format(getLocStr("black_market_auto_refresh"), remainingTimeDesc(leftTime)));

        performWithDelay(self, tick, 1);
    end

    tick();
end

-- 主动请求数据
function UIScrapShop:tryRefresh()
    Operation.cmd_try_refresh_scrap_shop();
    self.hasTry = self.hasTry + 1;
end

-- 碎片兑换信息
function UIScrapShop:getScrapInfo()
    -- 获取本店的所有商品
    local goodsList = PetShopM.getAllScrapGoods();
    if #goodsList <= 0 then
        return {};
    end

    local ret = {};
    local goodsIndex = 1;  -- 商品的序号
    for _, info in ipairs(goodsList) do
        local goodsId    = info["goods_id"];
        local payAttrib  = info["dungeon_pet_chips"];    -- 支付碎片
        local goodsPrice = info["dungeon_chips_cost"];   -- 支付数量
        local classId    = info["rare_pet_chips"];       -- 兑换碎片
        local icon       = ItemM.query(classId, "icon"); -- 碎片图标
        local name       = ItemM.query(classId, "name"); -- 碎片名称
        local num        = info["rare_chips_num"];       -- 兑换数量
        table.insert(ret, {goodsId, goodsId, classId, icon, payAttrib, goodsPrice,
               name, num});
    end

    return ret;
end

-- 罐子购买信息
function UIScrapShop:getTankInfo()
    -- 获取本店的所有商品
    local goodsList = PetShopM.getAllScrapGoods();
    if #goodsList <= 0 then
        return {};
    end

    -- 黄金罐信息
    local ret = {}
    local tankType = {"money", "gem"};

    for index, perType in ipairs(tankType) do
        local classId = FormulaM.invoke("CALC_PUZZLE_BOX_COST_CLASS_ID", perType);
        local cost = FormulaM.invoke("CALC_PUZZLE_BOX_COST", perType);
        local payAttrib = cost[2];
        local goodsPrice = cost[3];
        local icon = ItemM.query(classId, "icon");
        local name = ItemM.query(classId, "name");
        local num = 0;
        table.insert(ret, {index, index, classId, icon, payAttrib, goodsPrice, name, num});
    end

    return ret;
end

-- 获取可购买的商品信息
function UIScrapShop:gatherGoodsInfo()
    if self.shopType == PETSHOP_TYPE_SCRAP then
        -- 碎片兑换
        return self:getScrapInfo();
    elseif self.shopType == PETSHOP_TYPE_PUZZLE_BOX then
        -- 购买罐子
        return self:getTankInfo();
    else
        return {};
    end
end

-- 当商品被点击
function UIScrapShop:onGoodsClick(iconImg)
    if iconImg.soldOut then
        alert(getLocStr("goods_exchange_already"));
        return;
    end

    -- 显示商品详细信息
    if iconImg.goodsType == PETSHOP_TYPE_SCRAP then
        -- 兑换碎片
        UIMgr.getCurrentScene():removeFormByName("UIPetShopGoodsDetail");
        local UIPetShopGoodsDetail = UIPetShopGoodsDetail.create(iconImg.goodsId);
        UIMgr.getCurrentScene():addForm(UIPetShopGoodsDetail);
    elseif iconImg.goodsType == PETSHOP_TYPE_PUZZLE_BOX then
        -- 购买罐子
        local classId = FormulaM.invoke("CALC_PUZZLE_BOX_COST_CLASS_ID", iconImg.payAttrib);
        UIMgr.getCurrentScene():removeFormByName("UIBuyPuzzles");
        local uiform = UIBuyPuzzles.create(classId, iconImg.payAttrib);
        UIMgr.getCurrentScene():addForm(uiform);
    end
end

-- 注册商品点击事件
function UIScrapShop:registerPetShopTouchEvent(node)
    -- 注册商品图标点击事件
    local image = node:getChildByName("bg");
    local iconImg = node:getChildByName("goods_icon");
    local function onClick(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            image:runAction(cc.ScaleTo:create(0.1, 0.90));
        elseif eventType == ccui.TouchEventType.canceled then
            image:runAction(cc.ScaleTo:create(0.1, 1));
        elseif eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_spell");
            image:runAction(cc.ScaleTo:create(0.1, 1));
            -- 仅处理非空的格子
            if type(iconImg.goodsIcon) == 'number' then
                self:onGoodsClick(iconImg);
            end
        end
    end
    image:addTouchEventListener(onClick)
end

-- 注册事件处理回调函数
function UIScrapShop:registerEventCallback()
    -- 注册获得焦点的回调
    EventMgr.register("UIScrapShop", event.FOCUS_CHANGED, function(para)
        if para["get"] == "UIScrapShop" then
            UIMgr:getCurrentScene().topMenu:show(TOPMENU_FRAME_MONEY, TOPMENU_FRAME_GEM);
        end
    end);

    -- 关注购买商品的事件
    EventMgr.register("UIScrapShop", event.PET_SHOP_EXCHANGE, function(args)
        local goodsId = args.goodsId;

        self:whenBuyGoods(goodsId);
    end);

    -- 开放黄金罐购买
    EventMgr.register("UIScrapShop", event.USER_INFO_UPDATED, function(args)
        if args == "open_gem_puzzle_buy" then
            self:redraw();
        end
    end);

    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIScrapShop 界面析构清理");
            EventMgr.removeAll("UIScrapShop");

            if UIMgr:getCurrentScene():isOpen("UIGainItemPath") then
                local uiForm = UIMgr:getCurrentScene():getFormByName("UIGainItemPath");
                uiForm:redraw();
                uiForm:initData();
            else
                if UIMgr.getCurrentScene().mainMap then
                    UIMgr.getCurrentScene().mainMap:resumeEffect();
                end
            end
        elseif eventType == "enter" then
        end
    end);
end

-- 注册点击事件
function UIScrapShop:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIScrapShop");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册各个标签页点击事件
    for _, tabNode in pairs(self.tabs) do
        local iconImg = findChildByName(tabNode, "icon");
        local function onTabClick(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                AudioM.playFx("button_spell");
                self.shopType = sender.shopType;

                -- 重绘标签页
                self:redrawTabs();

                -- 重绘页面
                self:redraw();
            end
        end
        iconImg:addTouchEventListener(onTabClick);
    end
end

-- 关闭购买商品的消息
function UIScrapShop:whenBuyGoods(goodsId)
    -- 刷新界面
    self:redraw();

    if not goodsId then
        return;
    end

    -- 提示玩家购买了商品
    local function delay()
        local goodInfo = PetShopM.getGoodInfoInShop(goodsId);
        local count    = goodInfo["rare_chips_num"];
        local name     = ItemM.query(goodInfo["rare_pet_chips"], "name");
        alert(string.format(getLocStr("exchange_scraps_done"), name, count));
    end

    performWithDelay(self, delay, 0.1);
end

-- 重绘标签页
function UIScrapShop:redrawTabs()
    for _, tabNode in pairs(self.tabs) do
        local iconImg   = findChildByName(tabNode, "icon");
        local lightImg = findChildByName(tabNode, "light");
        local titleLabel = findChildByName(tabNode, "title");
        local shopType = iconImg.shopType;

        local kerning = 0;

        if not isFullWidthLang() then
            kerning = -3;
        end
        local tabStr = self:getTabNameMap()[shopType];
        TextStyleM.setOutlineStyle(titleLabel, TextStyleM.TEXT_OUTLINE_SIZE, TextStyleM.TEXT_OUTLINE_COLOR_DARK);
        TextStyleM.setTextStyle(titleLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true, kerning, nil, nil, nil, nil, nil, TextStyleM.getTextScale(tabStr, 6));

        titleLabel:setString(tabStr);
        local normalColor = TextStyleM.TEXT_COLOR_NORMAL;
        local blendColor  = TextStyleM.TEXT_COLOR_BLEND;

        if self.shopType == shopType then
            -- 被选中的标签高亮显示
            lightImg:setVisible(true);
            iconImg:setHighlighted(true);
            iconImg:setTouchEnabled(false);
            iconImg:setColor(normalColor);
            titleLabel:setColor(normalColor);
        else
            lightImg:setVisible(false);
            iconImg:setHighlighted(false);
            iconImg:setTouchEnabled(true);
            iconImg:setColor(blendColor);
            titleLabel:setColor(blendColor);
        end
    end
end

-- 页签动画效果
function UIScrapShop:playTabEffect()
    -- 带窗口动画播放结束后，页签从上面往下掉
    for i = 1, #self.tabs do
        local tab = self.tabs[i];
        local pos = cc.p(tab:getPosition());
        local delay = cc.DelayTime:create(0.2 * (i - 1));
        local moveDown = cc.MoveTo:create(0.2, cc.p(pos.x, pos.y - 100 - 5));
        local moveBack = cc.MoveTo:create(0.2, cc.p(pos.x, pos.y - 100));
        local action = cc.Sequence:create(delay, moveDown, moveBack);
        tab:runAction(action);
    end
end